<template><div><h2 id="redis" tabindex="-1"><a class="header-anchor" href="#redis"><span>Redis</span></a></h2>
<p>Yii2 框架的官方 <code v-pre>yii2-redis</code> 扩展为我们提供了 <a href="http://redis.io/" target="_blank" rel="noopener noreferrer">redis</a> 键值（key-value）存储的支持，也包含了一个 <code v-pre>Cache</code> 和 <code v-pre>Session</code> 存储处理器，并且实现了 <code v-pre>ActiveRecord</code> 模式，允许你在 redis 中存储活动记录等各种功能。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>redis &gt;= 2.6.12 版本，这个是该扩展所有部件正常工作所必需的条件。</p>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require yiisoft/yii2-redis</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者在 <code v-pre>composer.json</code> 文件中的 require 部分增加以下内容：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token property">"yiisoft/yii2-redis"</span><span class="token operator">:</span> <span class="token string">"~2.0.0"</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后执行 <code v-pre>composer install</code> 命令即可。</p>
<h2 id="应用组件配置" tabindex="-1"><a class="header-anchor" href="#应用组件配置"><span>应用组件配置</span></a></h2>
<p>使用此扩展时，需要在你的应用程序配置中配置 <code v-pre>yii\redis\Connection</code> 类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'hostname'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">6379</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token comment">// 用户名</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token comment">// 密码</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// 当连接失败时，应该重试命令执行的次数</span></span>
<span class="line">            <span class="token comment">// 要求 yiisoft/yii2-redis >=2.0.7</span></span>
<span class="line">            <span class="token string single-quoted-string">'retries'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="活动记录的使用" tabindex="-1"><a class="header-anchor" href="#活动记录的使用"><span>活动记录的使用</span></a></h2>
<p>对于如何使用 Yii 的活动记录一般信息请参阅 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md" target="_blank" rel="noopener noreferrer">指南</a>。</p>
<p>定义一个 redis 活动记录类，你的记录类需要继承自 <code v-pre>yii\redis\ActiveRecord</code> 并且至少实现 <code v-pre>attributes()</code> 方法来定义记录的属性。一个没有指定默认值，默认为 <code v-pre>id</code> 的主键可以通过 [[yii\redis\ActiveRecord::primaryKey()]] 定义。主键是属性中必要的一部分，所以请确保你有一个 <code v-pre>id</code> 属性定义的，如果你没有指定自己的主键。</p>
<p>以下是一个 <code v-pre>Customer</code> 的实例模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Customer</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>ActiveRecord</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span> 此记录的属性列表</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'registration_date'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">ActiveQuery</span> 定义一个关联到 Order 的记录（可以在其它数据库中，例如 elasticsearch 或者 sql）</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getOrders</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">hasMany</span><span class="token punctuation">(</span><span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">className</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'customer_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">CustomerQuery</span><span class="token punctuation">(</span><span class="token function">get_called_class</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CustomerQuery</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>ActiveQuery</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 定义一个修改 `$query` 的范围返回有效（status = 1）的客户。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">active</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">andWhere</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>redis 活动记录的一般用法和数据库活动记录非常相似，正如 <a href="https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md" target="_blank" rel="noopener noreferrer">指南</a> 中所描述的。它支持相同的界面和功能，除了以下限制：</p>
<ul>
<li>由于 redis 不支持 SQL 查询，因此查询 API 仅限于以下方法： <code v-pre>where()</code>，<code v-pre>limit()</code>，<code v-pre>offset()</code>，<code v-pre>orderBy()</code> 和 <code v-pre>indexBy()</code>。(orderBy() 尚未实现：<a href="https://github.com/yiisoft/yii2/issues/1305" target="_blank" rel="noopener noreferrer">#1305</a>)</li>
<li>由于 redis 中没有表，因此无法通过 <code v-pre>via</code> 表来定义关系。你只能通过其他记录来定义关系。</li>
</ul>
<p>另外，也可以定义从 redis 的活动记录到普通的活动记录的关系，反之亦然。</p>
<p>使用实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$customer</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Customer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$customer</span><span class="token operator">-></span><span class="token property">attributes</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'test'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$customer</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$customer</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span> <span class="token comment">// 如果没有明确设置 id 会自动递增</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$customer</span> <span class="token operator">=</span> <span class="token class-name static-context">Customer</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'test'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">one</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 通过 query 查找</span></span>
<span class="line"><span class="token variable">$customer</span> <span class="token operator">=</span> <span class="token class-name static-context">Customer</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">active</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 通过 query 查找全部（使用 `active` 范围）</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="直接使用命令行" tabindex="-1"><a class="header-anchor" href="#直接使用命令行"><span>直接使用命令行</span></a></h2>
<p>Redis 有很多可以直接从连接中使用的有用的命令。在配置应用程序后，如 <a href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide-zh-CN/installation.md" target="_blank" rel="noopener noreferrer">安装</a> 所示，连接可以像下面这样获取：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$redis</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">redis</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>完成之后可以执行如下命令。最通用的方法是使用 <code v-pre>executeCommand</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$redis</span><span class="token operator">-></span><span class="token function">executeCommand</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'hmset'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'test_collection'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'val1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'val2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>每个命令都有相应的快捷方式支持，所以可以像下面这样代替以上的命令：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$redis</span><span class="token operator">-></span><span class="token function">hmset</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'test_collection'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'val1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'key2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'val2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>可用命令列表和他们的参数可参阅 <a href="http://redis.io/commands" target="_blank" rel="noopener noreferrer">redis.io/commands</a>。</p>
<h2 id="缓存组件的使用" tabindex="-1"><a class="header-anchor" href="#缓存组件的使用"><span>缓存组件的使用</span></a></h2>
<p>为了使用 <code v-pre>Cache</code> 组件，如 <a href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide-zh-CN/installation.md" target="_blank" rel="noopener noreferrer">安装</a> 章节中所描述的，除了配置连接， 你也需要配置 [[yii\redis\Cache]] 中的 <code v-pre>cache</code> 组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Cache'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你只使用 redis 缓存（即，不使用它的活动记录或者会话），您还可以配置缓存组件内的 连接参数（在这种情况下，不需要配置连接应用程序的组件）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'cache'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Cache'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'hostname'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">6379</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="会话组件的使用" tabindex="-1"><a class="header-anchor" href="#会话组件的使用"><span>会话组件的使用</span></a></h2>
<p>为了使用 <code v-pre>Session</code> 组件，如 <a href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide-zh-CN/installation.md" target="_blank" rel="noopener noreferrer">安装</a> 章节中所描述的，除了配置连接， 你也需要配置 <code v-pre>yii\redis\Session</code> 中的 <code v-pre>session</code> 组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'session'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Session'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你只使用 redis 会话（即，不使用它的活动记录或者缓存），您还可以配置会话组件内的 连接参数（在这种情况下，不需要配置连接应用程序的组件）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">//....</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">        <span class="token string single-quoted-string">'session'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Session'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'advanced-api'</span><span class="token punctuation">,</span> <span class="token comment">// 当前的会话名称</span></span>
<span class="line">            <span class="token string single-quoted-string">'timeout'</span> <span class="token operator">=></span> <span class="token number">3600</span><span class="token punctuation">,</span> <span class="token comment">// 过期被清理的秒数</span></span>
<span class="line">            <span class="token string single-quoted-string">'keyPrefix'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'session:app:'</span><span class="token punctuation">,</span> <span class="token comment">// 缓存键的前缀字符串</span></span>
<span class="line">            <span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\redis\Connection'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'hostname'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">6379</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 会话cookie参数，可以用于同一级域名下的子域名项目之前共享 session</span></span>
<span class="line">            <span class="token comment">/*'cookieParams' => [</span>
<span class="line">                'path' => '/',</span>
<span class="line">                'httpOnly' => true,</span>
<span class="line">                'domain' => 'xxx.com',</span>
<span class="line">                'lifetime' => 3600,</span>
<span class="line">            ],*/</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


